$(document).ready(function(){
    var editing_index = -1;
    var editing_id = '';

    $('.resume_list .fa-edit').click(function(e){
        var index = $(e.target).closest('tr').index();

        if(editing_index == index){
            $('.input-contact-info .cancel').click();
            return;
        }

        $('#contact-info').val($(e.target).closest('tr').children('.contact_information').text());
        $('.input-contact-info').insertAfter($(e.target).closest('tr')).removeClass('hidden');
        editing_id = $(e.target).closest('tr').attr('id');
        editing_index = index;
    });

    $('.nav-tabs .nav-link').click(function(e){
        $('.nav-tabs .nav-link').removeClass('active');
        $(e.target).addClass('active');

        if($(e.target).attr('tar') == 'account-manager'){
            $('.account-manager').removeClass('hidden');
            $('.contact-manager').addClass('hidden');
        }else if($(e.target).attr('tar') == 'contact-manager'){
            $('.account-manager').addClass('hidden');
            $('.contact-manager').removeClass('hidden');
        }
    });

    // 账户管理
    $('.na-btn').click(function(){
        $('.na-container').toggleClass('hidden');
    });

    $('.na-cancel').click(function(e){
        $('.na-container').toggleClass('hidden');
        $('#na-name').val('');
        $('#na-pwd').val('');
    });

    $('.na-sure').click(function(e){
        var username = $('#na-name').val();
        var password = $('#na-pwd').val();

        $.ajax({
            'url':'/admin/account_manager.do',
            'data':{
                'action':'add',
                'username':username,
                'password':password
            },
            'success':function(resp){
                if(resp.success){
                    $('#na-name').val('');
                    $('#na-pwd').val('');

                    var item = resp.data;

                    $('<tr><th scope="row">' + ($('.account-manager table tbody tr').size() + 1) + '</th><td>'+item['username']+'</td><td>'+item['uid']+'</td><td>'+item['role']+'</td><td>'+item['ctime']+'</td></tr>').insertAfter($('.account-manager table tr:last'));
                }
            }
        });
    });

    $('.input-contact-info .cancel').click(function(e){
        $('.input-contact-info').addClass('hidden').insertAfter($('.resume_list tbody tr:last'));
        $('#contact-info').val('');
        editing_index = -1;
        editing_id = '';
    });

    $('.input-contact-info .sure').click(function(e){
        info = $('#contact-info').val();

//        if(info.length == 0){
//            return alert('联系信息不能为空');
//        }

        $.ajax({
            'url':'/admin/fill_contact_info.do',
            'data':{
                'id':editing_id,
                'info':info
            },
            'success':function(resp){
                if(resp.success){
                    $('.resume_list tbody tr').eq(editing_index).children('.contact_information').text(info);
                    $('.input-contact-info .cancel').click();
                }
            }
        });
    });
});